package com.sq.partner.manager.dao.impl;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.sq.partner.manager.dao.FeeRateDao;
import com.sq.partner.manager.mybatis.model.GatewayAuwConfig;
import com.sq.partner.manager.mybatis.model.GatewayAuwProd;
import com.sq.partner.manager.mybatis.model.RiskAlgMap;
import com.sq.partner.manager.mybatis.model.TradFeeRateAmnt;
import com.sq.partner.manager.mybatis.model.TradFeeRateDay;
import com.sq.partner.manager.mybatis.model.TradFeeRateFixed;
import com.sq.partner.manager.mybatis.model.TradFeeRateMonth;
import com.sq.partner.manager.mybatis.model.TradFeeRiskCommon;
import com.sq.partner.manager.util.LogUtil;

@Repository
public class FeeRateDaoImpl extends BaseDaoImpl implements FeeRateDao {

	private static Logger logger = LogUtil.getLog(FeeRateDaoImpl.class);

	@Override
	public void insertIntoFeeRateDay(TradFeeRateDay tradFeeRateDay) throws Exception {
		this.add("insertTradFeeRateDay", tradFeeRateDay);
	}

	@Override
	public <T> void batchInsertIntoFeeRate(String statementName, List<T> list) throws Exception {
		this.batchInsert(statementName, list);
	}

	@Override
	public boolean batchInsertIntoRiskCommon(List<TradFeeRiskCommon> list) {
		Connection conn = null;
		PreparedStatement pstmt = null;

		StringBuffer sb = new StringBuffer();
		sb.append("insert into t_trad_fee_risk_common (ID,RISK_CODE,PREM_RATE,COND1,COND2,COND7)");
		sb.append("values((select max(id)+1 from t_trad_fee_risk_common),?,?,?,?,?)");
		try {
			conn = this.getConnection();
			conn.setAutoCommit(false);

			pstmt = conn.prepareStatement(sb.toString());
			TradFeeRiskCommon riskCommon = null;
			for (int i = 0; i < list.size(); i++) {
				riskCommon = list.get(i);
				pstmt.setObject(1, riskCommon.getRiskCode());
				pstmt.setObject(2, riskCommon.getPremRate());
				pstmt.setObject(3, riskCommon.getCond1());
				pstmt.setObject(4, riskCommon.getCond2());
				pstmt.setObject(5, riskCommon.getCond7());
				pstmt.executeQuery();
			}
			conn.commit();
			return true;
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				logger.error("插入险种通用算法，数据回滚失败", e1);
				return false;
			}
			logger.error("插入险种通用算法失败", e);
			return false;
		} finally {
			if (pstmt != null) {
				try {
					pstmt.close();
				} catch (SQLException e) {
					logger.error("关闭pstmt失败", e);
					/*return false;*/
				}
				close(conn);
			}
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TradFeeRateDay> queryTradFeeRateDay(TradFeeRateDay tradFeeRateDay) throws Exception {
		return this.queryList("queryTradFeeRateDay", tradFeeRateDay);
	}

	@Override
	public void deleteTradFeeRateDayByRiskCode(String riskCode) throws Exception {
		this.delete("deleteTradFeeRateDayByRiskCode", riskCode);
	}

	@Override
	public void insertIntoFeeRateMonth(TradFeeRateMonth tradFeeRateMonth) throws Exception {
		this.add("insertTradFeeRateMonth", tradFeeRateMonth);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TradFeeRateMonth> queryTradFeeRateMonth(TradFeeRateMonth tradFeeRateMonth) throws Exception {
		return this.queryList("queryTradFeeRateMonth", tradFeeRateMonth);
	}

	@Override
	public void deleteTradFeeRateMonthByRiskCode(String riskCode) throws Exception {
		this.delete("deleteTradFeeRateMonthByRiskCode", riskCode);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TradFeeRateFixed> queryTradFeeRateFixedById(String riskCode) throws Exception {
		return this.queryList("queryTradFeeRateFixedById", riskCode);
	}

	@Override
	public void delTradFeeRateFixedById(String riskCode) throws Exception {
		this.delete("delTradFeeRateFixedById", riskCode);
	}

	@Override
	public void insertTradFeeRateFixed(TradFeeRateFixed feeRateFixed) throws Exception {
		this.add("insertTradFeeRateFixed", feeRateFixed);
	}

	// 根据险种id查询保额计算保费法表
	@Override
	@SuppressWarnings("unchecked")
	public List<TradFeeRateAmnt> queryTradFeeRateRateAmntById(String riskCode) throws Exception {
		return this.queryList("queryTradFeeRateRateAmntById", riskCode);
	}

	// 根据险种代码删除保额计算保费法表记录
	@Override
	public void delTradFeeRateAmntById(String riskCode) throws Exception {
		this.delete("delTradFeeRateAmntById", riskCode);
	}

	// 添加保额计算保费表
	@Override
	public void insertTradFeeRateAmnt(TradFeeRateAmnt tradFeeRateAmnt) throws Exception {
		this.add("insertTradFeeRateAmnt", tradFeeRateAmnt);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<GatewayAuwConfig> queryAuwConfigsByPage(Map<String, Object> param) throws Exception {
		return this.queryList("queryAuwConfigsByPage", param);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<GatewayAuwProd> queryAuwConfigsByProd(String tradProdCode) throws Exception {
		return this.queryList("queryAuwConfigsByProd", tradProdCode);
	}

	@Override
	public int queryTotalSizeOfAuwConfigs() throws Exception {
		return (Integer) this.query("queryTotalSizeOfAuwConfigs", new Object() {
		});
	}

	@Override
	public void removeAllAuwConfigsFromProd(String tradProdCode) throws Exception {
		this.delete("deleteAuwConfigsByProdCode", tradProdCode);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<TradFeeRiskCommon> queryTradFeeRiskCommonByMap(Map<String, Object> map) throws Exception {
		return this.queryList("queryTradFeeRiskCommonByMap", map);
	}

	/**
	 * 根据险种RiskCode删除表中记录
	 * 
	 */
	@Override
	public void delTradFeeRiskCommonByRiskCode(String tradPlanCode) throws Exception {
		this.delete("delTradFeeRiskCommonByRiskCode", tradPlanCode);
	}

	/**
	 * 查询数据库MaxId
	 * 
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public int queryMaxId() throws Exception {
		return (Integer) this.query("queryMaxId",new HashMap());
	}

	/**
	 * 添加险种通用算法(每天保费固定乘以天数)
	 * 
	 */
	@Override
	public void insertTradFeeRiskCommon(TradFeeRiskCommon tradFeeRiskCommon) throws Exception {
		this.add("insertTradFeeRiskCommon", tradFeeRiskCommon);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> queryTradFeeRiskCommonByRiskCode(String riskCode) throws Exception {
		return this.queryList("queryTradFeeRiskCommonByRiskCode", riskCode);
	}

	@Override
	public int checkOpenCalDiv(String tradPlanCode) throws Exception {
		return (Integer) this.query("queryCheckCount",tradPlanCode);
	}

	@Override
	public void updateOps(RiskAlgMap riskAlgMap) throws Exception {
		this.update("updateOps", riskAlgMap);
	}

	@Override
	public List<Map<String, Object>> impNlifeAuw(Map<String, Object> map)
			throws Exception {
		return this.queryList("impNlifeAuwByMap", map);
	}

	@Override
	public void deleteThisCal(String id) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		this.delete("deletThisCalByXBTConditions", id);
		
	}

	@Override
	public void updateThisCal(String prodCode, String riskCode, String id,
			String covCode, String cond1, String cond2, String cond3,
			String cond7, String cond10,String cond13,double premRate,String unused,String rationId) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("prodCode", prodCode);
		map.put("riskCode", riskCode);
		map.put("id", id);
		map.put("covCode", covCode);
		if(cond1==null||cond1=="undefined"){
			cond1="";
		}
		map.put("cond1", cond1);
		if(cond2==null||cond2=="undefined"){
			cond2="";
		}
		map.put("cond2", cond2);
		map.put("cond3", cond3);
		map.put("cond7", cond7);
		map.put("cond10", cond10);
		map.put("cond13", cond13);
		map.put("premRate", BigDecimal.valueOf(premRate/1000));
		
		map.put("unused", unused);
		map.put("rationId", rationId);
		
		this.update("updateThisCal", map);
		
	}

	@Override
	public void insertThisCal(Map<String, Object> map) throws Exception {
		
		this.add("insertThisCal", map);
		
	}

	@Override
	public String isExistCal(Map<String, String> map) throws Exception {
		return (String) this.query("isExistCal", map);
	}

	@Override
	public List<Map<String, BigDecimal>> queryCovUse3(Map<String, String> map) throws Exception {
		return this.queryList("queryCovUse3", map);
	}

	@Override
	public String writeBackShortRate(Map<String, String> map) throws Exception {
	
		return (String) this.query("writeBackShortRate", map);
	}

	@Override
	public int queryExistShortRateByMap(Map<String, String> map) throws SQLException {
		// TODO Auto-generated method stub
		return (Integer) this.query("queryExistShortRateByMap",map);
	}

	@Override
	public void updateShortRate(Map<String, String> map) throws Exception {
		
		this.update("updateShortRate", map);
		
	}


	
}
